﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entitty;

namespace ClassLibrary.DAO
{
    public class TblInforDAO
    {
          private DBService _db = null;
          public TblInforDAO(string ConnectionString)
         {
            _db = new DBService(ConnectionString);
         }
          public DataTable Insert(TblInformaton Info, int Mode)
          {
              DataTable dt = null;
              try
              {

                  dt = _db.ExeSP_DataTable("sp_Infor_InUp",
                      new string[] { "@Id_Info", "@Info_Title", "@Information", "@ID", "@ID_MenuItem", "@IDLang", "@mode" },
                      new object[] { Info.Id_Info, Info.Info_Title, Info.Information, Info.ID, Info.ID_MenuItem, Info.IdLang, Mode },
                      CommandType.StoredProcedure); 
                 
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return dt;
          }

          public TblInformaton GetInfo_ByMenu(int ID, int IDMenuItem)
          {
              TblInformaton info = new TblInformaton();
              try
              {
                  DataTable dt = null;
                  dt = _db.ExeSP_DataTable("select * from TblInfor where ID = @ID and ID_MenuItem = @IDMenuItem",
                      new string[] { "@ID", "@IDMenuItem" },
                      new object[] { ID, IDMenuItem }, CommandType.Text);
                  foreach (DataRow dr in dt.Rows)
                  {
                      info.Id_Info = Convert.ToInt32 (dr["Id_Info"].ToString());
                      info.ID = ID;
                      info.ID_MenuItem = IDMenuItem;
                      info.IdLang = Convert.ToInt32(dr["IDLang"].ToString());
                      info.Info_Title = dr["Info_Title"].ToString();
                      info.Information = dr["Information"].ToString();

                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return info;

          }

          public DataTable GetAll()
          {
              DataTable dt = new DataTable();
              try
              {
                  dt = _db.ExeSP_DataTable("select a.Id_Info, a.Information,a.Info_Title, b.MenuName from TblInfor a, TblMenu b where a.ID = b.ID", false, CommandType.Text);
                     
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return dt;
          }
        //Get Information By Id_Info
          public TblInformaton GetInfor_ByIdInfo(int Id_Infor)
            {
              TblInformaton info = new TblInformaton();
              try
              {
                  DataTable dt = null;
                  dt = _db.ExeSP_DataTable("select * from TblInfor where Id_Info = @Id_Infor",
                      new string[] { "@Id_Infor" },
                      new object[] { Id_Infor }, CommandType.Text);
                  foreach (DataRow dr in dt.Rows)
                  {
                      info.Id_Info = Id_Infor;
                      info.ID = Convert.ToInt32(dr["ID"].ToString());
                      info.ID_MenuItem = Convert.ToInt32(dr["ID_MenuItem"].ToString());
                      info.IdLang = Convert.ToInt32(dr["IDLang"].ToString());
                      info.Info_Title = dr["Info_Title"].ToString();
                      info.Information = dr["Information"].ToString();

                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return info;

          }
    }
}
